package bgpv1

import (
	"fmt"
	"topobuild/utils"

	"github.com/xuri/excelize/v2"
)

/*
设备名称	本端AS域	network地址	对端AS域	对端接口IP
R2001	10	172.30.201.168/32	20	172.30.254.94
R2002	20	192.168.201.168/32	10	172.30.254.93
*/

type TableBGPv1Unicast struct {
	DevName string `json:"dev_name"`
	As      int    `json:"as"`
	Net     string `json:"net"`
	NeiAs   int    `json:"nei_as"`
	NeiIp   string `json:"nei_ip"`
}

// 将TableBGPv1Unicast中的数据,写入到指定xlsx文件的unicast表中
func WriteUnicastXlsx(filename string, unis []TableBGPv1Unicast) {
	// 如果filename已经存在,则直接打开,否则创建一个新文件
	utils.CheckFile(filename)
	xlsx, err := excelize.OpenFile(filename)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer xlsx.Close()
	// Create a new sheet.
	index, err := xlsx.NewSheet("unicast")
	if err != nil {
		fmt.Println(err)
		return
	}
	// Set value of a cell.
	xlsx.SetCellValue("unicast", "A1", "设备名称")
	xlsx.SetCellValue("unicast", "B1", "本端AS域")
	xlsx.SetCellValue("unicast", "C1", "network地址")
	xlsx.SetCellValue("unicast", "D1", "对端AS域")
	xlsx.SetCellValue("unicast", "E1", "对端接口IP")
	// 设置单元格样式
	// style, _ := xlsx.NewStyle(`{"alignment":{"horizontal":"center"}}`)
	// xlsx.SetCellStyle("dev", "A1", "G1", style)
	// 设置列宽
	// xlsx.SetColWidth("dev", "A", "G", 20)
	// 设置行高
	// Set active sheet of the workbook.
	xlsx.SetActiveSheet(index)
	// Save xlsx file by the given path.
	fmt.Printf("len(unis): %v\n", len(unis))
	for i, uni := range unis {
		// fmt.Printf("uni: %v\n", uni)
		r := i + 2
		xlsx.SetCellValue("unicast", fmt.Sprintf("A%v", r), uni.DevName)
		xlsx.SetCellValue("unicast", fmt.Sprintf("B%v", r), uni.As)
		xlsx.SetCellValue("unicast", fmt.Sprintf("C%v", r), uni.Net)
		xlsx.SetCellValue("unicast", fmt.Sprintf("D%v", r), uni.NeiAs)
		xlsx.SetCellValue("unicast", fmt.Sprintf("E%v", r), uni.NeiIp)
	}
	xlsx.Save()
}
